﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eassets.Layered.Domain.Global;
using Eassets.Service.Global;
using Eassets.Service.Secure;
using System.Collections.ObjectModel;

namespace Eassets.UI.GlobalReference
{
    public partial class TransactionTypeForm : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                textBoxTransactionTypeCode.Attributes.Add("onChange", "javascript:this.value=toUpperCase(this.value);");
                textBoxTransactionTypeCode.Attributes.Add("onKeyPress", "javascript:return validateCodeForSymbols(event)");
                textBoxDescription.Attributes.Add("onChange", "javascript:this.value=toTitleCase(this.value)");

                //LoadModules();
                LoadTransactionTypes();

                ViewState["ButtonBehaviour"] = "Load";
                DisableComponants();
            }
        }


        protected void LoadTransactionTypes()
        {
            TransactionTypeService transactionTypeService = new TransactionTypeService();
            Collection<TransactionType> transactionTypes = new Collection<TransactionType>();
            transactionTypes = transactionTypeService.GetAllTransactionType();
            gridViewTransactionType.DataSource = transactionTypes;
            gridViewTransactionType.DataBind();
        }

        //private TransactionType GetTransactionTypes(int TransactionTypeId)
        //{
        //    TransactionTypeService transactiontypeservice = new TransactionTypeService();
        //    OpenArc.Erims.Domain.Ecorp.TransactionType transactiontype = new OpenArc.Erims.Domain.Ecorp.TransactionType();
        //    transactiontypeservice.GetTransactionTypeById(TransactionTypeId);

        //    return transactiontype;
        //}



        protected void gridViewTransactionType_SelectedIndexChanged(object sender, EventArgs e)
        {
            TransactionType transactionType = new TransactionType();
            TransactionTypeService transationtypeservice = new TransactionTypeService();

            GridViewRow row = gridViewTransactionType.SelectedRow;
            hiddenFieldTransactionTypeId.Value = row.Cells[1].Text;

            transactionType = transationtypeservice.GetByIdTransactionType(Convert.ToInt64(hiddenFieldTransactionTypeId.Value));
            textBoxTransactionTypeCode.Text = row.Cells[2].Text;
            textBoxDescription.Text = row.Cells[3].Text;

            ViewState["ButtonBehaviour"] = "GridSelectedChanged";
            DisableComponants();

        }

        protected void gridViewTransactionType_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 1)
            {
                e.Row.Cells[1].Visible = false;
            }
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            

            if (Convert.ToBoolean(ViewState["checkAddModify"]) == true)
            {
                if (CheckMandatory("A"))
                {
                    TransactionType transactionType = new TransactionType();
                    TransactionTypeService transactiontypeservice = new TransactionTypeService();
                    transactionType.Code = textBoxTransactionTypeCode.Text.ToString();
                    transactionType.Description = textBoxDescription.Text.ToString();

                    try
                    {
                        if (Page.IsValid)
                        {
                            transactiontypeservice.AddTransactionType(transactionType);
                            FlashMessage1.Message = "Successfully Saved";
                            FlashMessage1.CssClass = "okmessage";
                            FlashMessage1.Display();
                            LoadTransactionTypes();
                          
                        }
                    }
                    catch (Exception exception)
                    {

                        FlashMessage1.Message = "Saving Failed";
                        FlashMessage1.CssClass = "errormessage";
                        FlashMessage1.Display();
                    }

                    //LoadTransactionTypesByModule();
                    clearInputs();
                }
            }
            else
            {
                if (hiddenFieldTransactionTypeId.Value != String.Empty)
                {
                    if (CheckMandatory("M"))
                    {
                        TransactionType transactionType = new TransactionType();
                        TransactionTypeService transactiontypeservice = new TransactionTypeService();
                        transactionType.TransactionTypeId = Convert.ToInt64(hiddenFieldTransactionTypeId.Value);
                        transactionType.Code = textBoxTransactionTypeCode.Text.ToString();
                        transactionType.Description = textBoxDescription.Text.ToString();

                        try
                        {
                            if (Page.IsValid)
                            {
                                transactiontypeservice.ModifyTransactionType(transactionType);
                                FlashMessage1.Message = "Successfully Saved";
                                FlashMessage1.CssClass = "okmessage";
                                FlashMessage1.Display();
                                LoadTransactionTypes();
                            }
                        }
                        catch (Exception exception)
                        {
                            FlashMessage1.Message = "Saving Failed";
                            FlashMessage1.CssClass = "errormessage";
                            FlashMessage1.Display();
                        }

                        //LoadTransactionTypesByModule();
                        clearInputs();
                    }
                    else
                    {
                        FlashMessage1.Message = "Select a record to Modify!";
                        FlashMessage1.CssClass = "errormessage";
                        FlashMessage1.Display();
                    }
                }
            }

            ViewState["ButtonBehaviour"] = "Save";
            DisableComponants();
        }







        protected void buttonCancel_Click(object sender, EventArgs e)
        {
            clearInputs();

        }

        //protected void buttonSaveModify_Click(object sender, EventArgs e)
        //{
        //    if (hiddenFieldTransactionTypeId.Value != String.Empty)
        //    {
        //        if (CheckMandatory("M"))
        //        {
        //            TransactionType transactionType = new TransactionType();
        //            TransactionTypeService transactiontypeservice = new TransactionTypeService();
        //            transactionType.TransactionTypeId = Convert.ToInt64(hiddenFieldTransactionTypeId.Value);
        //            transactionType.Code = textBoxTransactionTypeCode.Text.ToString();
        //            transactionType.Description = textBoxDescription.Text.ToString();

        //            try
        //            {
        //                if (Page.IsValid)
        //                {
        //                    transactiontypeservice.ModifyTransactionType(transactionType);
        //                    FlashMessage1.Message = "Successfully Saved";
        //                    FlashMessage1.CssClass = "okmessage";
        //                    FlashMessage1.Display();
        //                }
        //            }
        //            catch (Exception exception)
        //            {
        //                FlashMessage1.Message = "Saving Failed";
        //                FlashMessage1.CssClass = "errormessage";
        //                FlashMessage1.Display();
        //            }

        //            //LoadTransactionTypesByModule();
        //            clearInputs();
        //        }
        //    }
        //    else
        //    {
        //        FlashMessage1.Message = "Select a record to Modify!";
        //        FlashMessage1.CssClass = "errormessage";
        //        FlashMessage1.Display();
        //    }

        //}

        protected void buttonDelete_Click(object sender, EventArgs e)
        {
            if (hiddenFieldTransactionTypeId.Value != String.Empty)
            {
                TransactionTypeService transactiontypeservice = new TransactionTypeService();

                try
                {
                    if (Page.IsValid)
                    {
                        transactiontypeservice.DeleteTransactionType(Convert.ToInt64(hiddenFieldTransactionTypeId.Value));
                        FlashMessage1.Message = "Successfully Deleted";
                        FlashMessage1.CssClass = "okmessage";
                        FlashMessage1.Display();
                    }
                }
                catch (Exception exception)
                {
                    FlashMessage1.Message = "Deleting Failed";
                    FlashMessage1.CssClass = "errormessage";
                    FlashMessage1.Display();
                }

                //LoadTransactionTypesByModule();
                clearInputs();
            }

        }

        protected void buttonCancelModify_Click(object sender, EventArgs e)
        {
            clearInputs();

        }
        private Boolean CheckMandatory(string addOrModify)
        {
            if (addOrModify == "A")
            {
                if (textBoxTransactionTypeCode.Text == String.Empty)
                {
                    FlashMessage1.Display("[Transaction Type Code] cannot be blank", "errormessage");
                    return false;
                }
                else if (textBoxDescription.Text == String.Empty)
                {
                    FlashMessage1.Display("[Description] cannot be blank", "errormessage");
                    return false;
                }
            }
            else if (addOrModify == "M")
            {
                if (textBoxTransactionTypeCode.Text == String.Empty)
                {
                    FlashMessage1.Display("[Transaction Type Code] cannot be blank", "errormessage");
                    return false;
                }
                else if (textBoxDescription.Text == String.Empty)
                {
                    FlashMessage1.Display("[Description] cannot be blank", "errormessage");
                    return false;
                }
            }
            return true;
        }

        private void clearInputs()
        {
            textBoxDescription.Text = "";
            textBoxTransactionTypeCode.Text = "";
            textBoxTransactionTypeCode.Text = "";
            textBoxDescription.Text = "";

            hiddenFieldTransactionTypeId.Value = String.Empty;


        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            ViewState["ButtonBehaviour"] = "Add";
            ViewState["checkAddModify"] = true;
            DisableComponants();
        }

        protected void ButtonModify_Click(object sender, EventArgs e)
        {
            ViewState["ButtonBehaviour"] = "Modify";
            ViewState["checkAddModify"] = false;
            DisableComponants();
        }

        protected void ButtonDelete_Click(object sender, EventArgs e)
        {

        }

        //protected void ButtonSave_Click(object sender, EventArgs e)
        //{

        //}

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            ViewState["ButtonBehaviour"] = "Cancel";
            DisableComponants();
        }

        protected void DisableComponants()
        {
            if (ViewState["ButtonBehaviour"] == "Add")
            {
                buttonModify.Enabled = false;

                buttonDelete.Enabled = false;

                textBoxTransactionTypeCode.Enabled = true;
                textBoxTransactionTypeCode.Text = String.Empty;
                textBoxDescription.Enabled = true;
                textBoxDescription.Text = string.Empty;
            }
            else if (ViewState["ButtonBehaviour"] == "Modify")
            {
                buttonAdd.Enabled = false;
                buttonDelete.Enabled = false;
                textBoxTransactionTypeCode.Enabled = true;
                textBoxDescription.Enabled = true;

            }
            else if (ViewState["ButtonBehaviour"] == "Delete")
            {

            }
            else if (ViewState["ButtonBehaviour"] == "Save")
            {
                textBoxTransactionTypeCode.Enabled = false;
                textBoxTransactionTypeCode.Text = string.Empty;
                textBoxDescription.Enabled = false;
                textBoxDescription.Text = string.Empty;
                buttonAdd.Enabled = true;

                buttonModify.Enabled = false;
                buttonDelete.Enabled = false;
                buttonDelete.Enabled = false;

            }
            else if (ViewState["ButtonBehaviour"] == "Cancel")
            {
                textBoxTransactionTypeCode.Enabled = false;
                textBoxTransactionTypeCode.Text = string.Empty;
                textBoxDescription.Enabled = false;
                textBoxDescription.Text = string.Empty;
                buttonAdd.Enabled = true;

                buttonModify.Enabled = false;
                buttonDelete.Enabled = false;
                buttonDelete.Enabled = false;
            }

            else if (ViewState["ButtonBehaviour"] == "Load")
            {
                textBoxTransactionTypeCode.Enabled = false;
                textBoxTransactionTypeCode.Text = string.Empty;
                textBoxDescription.Enabled = false;
                textBoxDescription.Text = string.Empty;
                buttonAdd.Enabled = true;

                buttonModify.Enabled = false;
                buttonDelete.Enabled = false;
                buttonDelete.Enabled = false;

            }
            else if (ViewState["ButtonBehaviour"] == "GridSelectedChanged")
            {
                buttonAdd.Enabled = false;
                buttonModify.Enabled = true;
                buttonDelete.Enabled = true;
            }

        }
    }

}